import type { Context } from 'koa';
import type { ResponseError } from '@/typings/global';

export const createResponse = (ctx: Context, handler: Function) => {
  return (...args: any[]) => handler(ctx, ...args);
}

export const sendSuccess = (ctx: Context, data: any = null) => {
  ctx.body = {
    code: 200,
    data: data,
    message: 'success'
  }
}

export const sendError = (ctx: Context, error: ResponseError) => {
  ctx.body = {
    ...error,
    data: null
  }
}

export const sendBadRequest = (ctx: Context, message: ResponseError['message']) => {
  return sendError(ctx, { code: 400, message });
}